package com.zhangshitong.springbootModel.demo.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.ss.usermodel.Workbook;
import org.jeecgframework.poi.excel.ExcelExportUtil;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.zhangshitong.springbootModel.base.BaseController;
import com.zhangshitong.springbootModel.base.DTO;
import com.zhangshitong.springbootModel.base.Page;
import com.zhangshitong.springbootModel.base.ResMessage;
import com.zhangshitong.springbootModel.demo.entity.User;
import com.zhangshitong.springbootModel.demo.service.UserService;
import com.zhangshitong.springbootModel.route.DemoRoute;

/**
 * 具有restful风格的demo 控制器编写
 * @author Administrator
 *
 */
@RestController
@RequestMapping(DemoRoute.ROOT)
public class UserController extends BaseController{

	@Autowired
	@Qualifier("userService")
	UserService userService;
	
	/**
	 * 新增user信息
	 * @param dto
	 * @return
	 */
	@PostMapping(value="/user")
	public ResMessage addUser(@RequestBody DTO dto){
		User user = null;
		try {
			user = (User) dto.getConverObject(User.class);
			if(null != user){
				Object rData = userService.saveUser(user);
				System.out.println(rData);
				return SuccessMessage(rData);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return FaultMessage("保存失败");
	}
	
	/**
	 * 编辑user信息
	 * @param dto
	 * @return
	 */
	@PutMapping(value="/user")
	public ResMessage editUser(@RequestBody DTO dto){
		User user = null;
		try {
			user = (User) dto.getConverObject(User.class);
			if(null != user){
				Object rData = userService.editUser(user);
				return SuccessMessage(rData);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return FaultMessage("编辑失败");
	}
	
	@DeleteMapping(value="/user")
	public ResMessage delUser(@RequestBody DTO dto){
		User user = null;
		try {
			user = (User) dto.getConverObject(User.class);
			if(null != user){
				String id = user.getId();
				Object rData = userService.delUser(id);
				return SuccessMessage(rData);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return FaultMessage("删除失败");
	}
	
	@RequestMapping(value="/user/{page}/{limit}",method={RequestMethod.GET,RequestMethod.POST})
	public ResMessage queryPage(@RequestBody DTO dto,@PathVariable(required=true)int page,@PathVariable(required=true)int limit){
		//Page p = getPageModel(request);
		Page p = new Page();
		p.setCurrentPage(page);
		p.setPageSize(limit);
		User user = null;
		try {
			user = (User) dto.getConverObject(User.class);
			if(null != user){
				return SuccessMessage(userService.queryUsers(p,user));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return FaultMessage("查询失败");
	}
	
	/**
	 * 导出全部数据(也可选择只导出当前页的数据)
	 * @param request
	 * @param response
	 */
	@RequestMapping(DemoRoute.EXPORT)
	public void export(HttpServletRequest request,HttpServletResponse response){
		try {
			response.setContentType("application/vnd.ms-excel");
			response.setHeader("Content-Disposition", "attachment;fileName=user_export.xls");
			Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("Demo","Sheet1"), User.class,userService.queryAll(null));
			workbook.write(response.getOutputStream());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
